package com.gitee.sparta.core.listener;

import com.gitee.sparta.common.IdentityEnum;
import com.gitee.sparta.core.bean.event.LeaderEnableEvent;
import com.gitee.sparta.core.cluster.ClusterCache;
import com.gitee.sparta.core.process.workers.SpartaCarryWorker;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;


/**
 * @author zyred
 * @since v 1.0.0
 */
@Slf4j
@AllArgsConstructor
public class LeaderEnableEventListener<V> implements ApplicationListener<LeaderEnableEvent> {

    private final SpartaCarryWorker<V> spartaCarryWorker;


    @SuppressWarnings("all")
    @Override
    public void onApplicationEvent(LeaderEnableEvent e) {
        if (IdentityEnum.leader()) {
            // 启动 carry 线程
            this.spartaCarryWorker.start();
            log.info("[Sparta] Enable carry, identity : {}", ClusterCache.getServerIdentity());
        }
    }
}
